
#############
##
## DATE (DEATHS DISTRIBUTED)
##
#############

ged <- read.csv("ged_main.csv") 
only_civ <- ged[which(ged$side_b=="Civilians"),]
library(ggtext)

subsetting_2 <- function(side, ...) {
  civil <- only_civ[which(only_civ$side_a==side),] %>%
    add_row(date_start = "2016-01-01", date_end = "2016-01-01", .before = 1) %>%
    add_row(date_start = "2019-11-30", date_end = "2019-11-30") %>%
    mutate(across(c(date_start, date_end), ymd)) %>% 
    transmute(id, date = map2(date_start, date_end, seq, by = 'day'), deaths_a, deaths_b, deaths_civilians) %>% 
    mutate(across(c(deaths_a, deaths_b, deaths_civilians), ~ ./lengths(date))) %>%
    unnest(date)  
  civil <- pad(civil) 
  # civil[is.na(civil)] <- 0
  return(civil)
}

plotting <- function(mydata, area, lower, upper){
  ggplot(mydata, aes(x=date, y=deaths_civilians)) +
    geom_point(size = 0.75) +  theme_classic() + ggtitle(area) +  
    scale_y_continuous(limits=c(lower, upper)) + 
    labs(x = " ", y = "Civilian Deaths", color = " ", size=0.8) +
    theme(axis.text=element_text(size = 12))
}

govciv <- subsetting_2(side="Government of Syria")
turciv <- subsetting_2(side="Government of Turkey")
isciv  <- subsetting_2(side="IS")
sdfciv <- subsetting_2(side="SDF")
opciv  <- subsetting_2(side="Syrian insurgents")


govciv_plot3 <- plotting(govciv, "Syrian Government vs. Civilians", 0.5,35)
turciv_plot3 <- plotting(turciv, "Turkish Government vs. Civilians", 0.5,25)
isciv_plot3  <- plotting(isciv, "ISIS vs. Civilians", 0.5,610)
sdfciv_plot3 <- plotting(sdfciv, "SDF vs. Civilians", 0.5,25)
opciv_plot3  <- plotting(opciv, "Opposition vs. Civilians", 0.5,25)

#############
##
## DATE START
##
#############

subsetting_3 <- function(side, ...) {
  civil <- only_civ[which(only_civ$side_a==side),] %>%
    add_row(date_start = "2016-01-01", date_end = "2016-01-01", .before = 1) %>%
    add_row(date_start = "2019-11-30", date_end = "2019-11-30") %>%
    mutate(across(c(date_start, date_end), ymd))
  civil <- pad(civil, by="date_start") 
  # civil$deaths_civilians[is.na(civil$deaths_civilians)] <- 0
  return(civil)
}

plotting <- function(mydata, area, lower, upper){
  ggplot(mydata, aes(x=date_start, y=deaths_civilians)) +
    geom_point(size = 0.75) +  theme_classic() + ggtitle(area) +  
    scale_y_continuous(name="Civilian Deaths", limits=c(lower, upper)) + 
    labs(x = " ", y = "Civilian Deaths", color = " ", size=0.8) +
    theme(axis.text=element_text(size = 12))
}

govciv3 <- subsetting_3(side="Government of Syria")
turciv3 <- subsetting_3(side="Government of Turkey")
isciv3 <- subsetting_3(side="IS")
sdfciv3 <- subsetting_3(side="SDF")
opciv3 <- subsetting_3(side="Syrian insurgents")

govciv_plot1 <- plotting(govciv3, "Syrian Government vs. Civilians", 0.5,35)
turciv_plot1 <- plotting(turciv3, "Turkish Government vs. Civilians", 0.5,25)
isciv_plot1 <- plotting(isciv3, "ISIS vs. Civilians", 0.5,610)
sdfciv_plot1 <- plotting(sdfciv3, "SDF vs. Civilians", 0.5,25)
opciv_plot1 <- plotting(opciv3, "Opposition vs. Civilians", 0.5,25)

#############
##
## DATE END
##
#############


subsetting_4 <- function(side, ...) {
  civil <- only_civ[which(only_civ$side_a==side),] %>%
    add_row(date_start = "2016-01-01", date_end = "2016-01-01", .before = 1) %>%
    add_row(date_start = "2019-11-30", date_end = "2019-11-30") %>%
    mutate(across(c(date_start, date_end), ymd))
  civil <- pad(civil, by="date_end") 
  # civil$deaths_civilians[is.na(civil$deaths_civilians)] <- 0
  return(civil)
}

plotting <- function(mydata, area, lower, upper){
  ggplot(mydata, aes(x=date_end, y=deaths_civilians)) +
    geom_point(size = 0.75) +  theme_classic() + ggtitle(area) +  
    scale_y_continuous(limits=c(lower, upper)) + 
    labs(x = " ", y = "Civilian Deaths", color = " ", size=0.8) +
    theme(axis.text=element_text(size = 12))
}

govciv4 <- subsetting_4(side="Government of Syria")
turciv4 <- subsetting_4(side="Government of Turkey")
isciv4  <- subsetting_4(side="IS")
sdfciv4 <- subsetting_4(side="SDF")
opciv4  <- subsetting_4(side="Syrian insurgents")

govciv_plot2 <- plotting(govciv4, "Syrian Government vs. Civilians", 0.5,35)
turciv_plot2 <- plotting(turciv4, "Turkish Government vs. Civilians", 0.5,25)
isciv_plot2  <- plotting(isciv4, "ISIS vs. Civilians", 0.5,610)
sdfciv_plot2 <- plotting(sdfciv4, "SDF vs. Civilians", 0.5,25)
opciv_plot2  <- plotting(opciv4, "Opposition vs. Civilians", 0.5,25)


big.plot <- grid.arrange(arrangeGrob(govciv_plot1  + ggtitle("<b><span style='color:#D55E00;'>Column 1:</span></b>
                                                 <br>Death Counts are Plotted on
                                                 <br>the <span style='color:#D55E00;'>Start</span> Date of an Event", 
                                                             subtitle = "Syrian Government vs. Civilians") + 
                                       theme(plot.title = element_textbox_simple(
                                         size = 13,
                                         lineheight = 1.1,
                                         padding = margin(5.5, 5.5,5.5, 5.5),
                                         margin = margin(5.5, 0,10, 0),
                                         fill = "cornsilk",
                                         halign = 0.5),plot.subtitle=element_text(size = 12),axis.title.y.left=element_text(size = 12)), 
                                     govciv_plot2  + ggtitle("<b><span style='color:#D55E00;'>Column 2:</span></b>
                                                 <br>Death Counts are Plotted on
                                                 <br>the <span style='color:#D55E00;'>End</span> Date of an Event", 
                                                             subtitle = "Syrian Government vs. Civilians") + labs(y = " ") + 
                                       theme(plot.title = element_textbox_simple(
                                         size = 13,
                                         lineheight = 1.1,
                                         padding = margin(5.5, 5.5,5.5, 5.5),
                                         margin = margin(5.5, 0,10, 0),
                                         fill = "cornsilk",
                                         halign = 0.5),plot.subtitle=element_text(size = 12)), 
                                     govciv_plot3  + ggtitle("<b><span style='color:#D55E00;'>Column 3:</span></b>
                                                 <br>Deaths Counts are <span style='color:#D55E00;'>Distributed</span>
                                                 <br>between Start and End Dates", 
                                                             subtitle = "Syrian Government vs. Civilians") + 
                                       labs(y = " ") +
                                       scale_y_continuous(limits=c(0.5,35), sec.axis = dup_axis(labels = NULL,
                                                                                                name= "<b><span style='color:#0072B2;'>Row 1:</span></b>
                           <br>Syrian Government One-Sided Violence 
                           <br>Number of Events: 31
                           <br>Max Deaths per event: 34")) +
                                       theme(plot.title = element_textbox_simple(
                                         size = 13,
                                         lineheight = 1.1,
                                         padding = margin(5.5, 5.5,5.5, 5.5),
                                         margin = margin(5.5, 0,10, 0),
                                         fill = "cornsilk",
                                         halign = 0.5), plot.subtitle=element_text(size = 12),
                                         axis.title.y.right= element_textbox_simple(
                                           size = 13,
                                           lineheight = 1.1,
                                           padding = margin(5.5, 5.5,5.5, 5.5),
                                           margin = margin(5.5, 0,10, 0), 
                                           halign = 0.5,
                                           minwidth = unit(1, "in"),
                                           maxwidth = unit(2.5, "in"), 
                                           fill = "lightsteelblue1"),
                                         axis.ticks.y.right= element_blank(),axis.line.y.right= element_blank()),
                                     opciv_plot1 + theme(plot.title = element_text(size = 12),axis.title.y.left=element_text(size = 12)),  
                                     opciv_plot2 + labs(y = " ") + theme(plot.title = element_text(size = 12)),  
                                     opciv_plot3 + labs(y = " ") + 
                                       theme(plot.title = element_text(size = 12),plot.subtitle=element_text(size = 12),
                                             axis.title.y.right= element_textbox_simple(
                                               size = 13,
                                               lineheight = 1.1,
                                               padding = margin(5.5, 5.5,5.5, 5.5),
                                               margin = margin(5.5, 0,10, 0), 
                                               halign = 0.5,
                                               minwidth = unit(1, "in"),
                                               maxwidth = unit(2.5, "in"), 
                                               fill = "lightsteelblue1"),
                                             axis.ticks.y.right= element_blank(),axis.line.y.right= element_blank()) +
                                       scale_y_continuous(limits=c(0.5,25), sec.axis = dup_axis(labels = NULL,
                                                                                                name= "<b><span style='color:#0072B2;'>Row 2:</span></b>
                           <br>Opposition One-Sided Violence 
                           <br>Number of Events: 110
                           <br>Max Deaths per event: 7")),
                                     isciv_plot1 + theme(plot.title = element_text(size = 12),axis.title.y.left=element_text(size = 12)),  
                                     isciv_plot2 + labs(y = " ") + theme(plot.title = element_text(size = 12)),  
                                     isciv_plot3 + labs(y = " ") + theme(plot.title = element_text(size = 12),
                                                                         axis.title.y.right= element_textbox_simple(
                                                                           size = 13,
                                                                           lineheight = 1.1,
                                                                           padding = margin(5.5, 5.5,5.5, 5.5),
                                                                           margin = margin(5.5, 0,10, 0), 
                                                                           halign = 0.5,
                                                                           minwidth = unit(1, "in"),
                                                                           maxwidth = unit(2.5, "in"), 
                                                                           fill = "lightsteelblue1"),
                                                                         axis.ticks.y.right= element_blank(),axis.line.y.right= element_blank()) +
                                       scale_y_continuous(limits=c(0.5,600), sec.axis = dup_axis(labels = NULL,
                                                                                                 name= "<b><span style='color:#0072B2;'>Row 3:</span></b>
                           <br>ISIS One-Sided Violence 
                           <br>Number of Events: 229
                           <br>Max Deaths per event: 600")),
                                     sdfciv_plot1 + theme(plot.title = element_text(size = 12),axis.title.y.left=element_text(size = 12)), 
                                     sdfciv_plot2 + labs( y = " ") + theme(plot.title = element_text(size = 12)), 
                                     sdfciv_plot3 + labs( y = " ") + 
                                       theme(plot.title = element_text(size = 12),
                                             axis.title.y.right= element_textbox_simple(size = 13,
                                                                                        lineheight = 1.1,
                                                                                        padding = margin(5.5, 5.5,5.5, 5.5),
                                                                                        margin = margin(5.5, 0,10, 0), 
                                                                                        halign = 0.5,
                                                                                        minwidth = unit(1, "in"),
                                                                                        maxwidth = unit(2.5, "in"), 
                                                                                        fill = "lightsteelblue1"),
                                             axis.ticks.y.right= element_blank(),,axis.line.y.right= element_blank()) +
                                       scale_y_continuous(limits=c(0.5,25), sec.axis = dup_axis(labels = NULL,
                                                                                                name= "<b><span style='color:#0072B2;'>Row 4:</span></b>
                           <br>SDF One-Sided Violence 
                           <br>Number of Events: 26
                           <br>Max Deaths per event: 6")), 
                                     ncol=3,heights=c(16,11,11,11),widths=c(11,11,20)))

ggsave("OSVcounts.pdf", plot = big.plot ,width=15, height=10,units = "in")  